package org.hhchat.leetcode.sort;

import java.util.List;

/**
 * Created this one by HMH on 2017/5/29.
 */
public class code524 {
    public String findLongestWord(String s, List<String> d) {
        String longest = "";
        for (String word : d)
            if (isBetter(word, longest) && isSubSequence(word, s))
                longest = word;
        return longest;
    }

    private boolean isBetter(String a, String b) {
        return a.length() > b.length() ||
                a.length() == b.length() && a.compareTo(b) < 0;
    }

    private boolean isSubSequence(String a, String b) {
        int start = -1;
        for (int i = 0; i < a.length(); i++){
            start = b.indexOf(a.charAt(i), start + 1);
            if (start < 0)
                return false;
        }
        return true;
    }
}
